const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const createBabelConfig = require('babel-preset-umi').default;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const babelPluginImport = require.resolve('babel-plugin-import');
const theme = require('./../src/theme');

const devMode = process.env.NODE_ENV !== 'production';
const publicPath = process.env.PUBLIC_PATH;
const backEndEnv = process.env.BACK_END_ENV;
const isAnalysis = process.env.ANALYSIS;

const urlEnv = { dev: 'dev', qa: 'qa', prod: '' }[backEndEnv.split('-')[0]];

const babelConfig = createBabelConfig();

babelConfig.presets.push([
  '@babel/preset-typescript',
  {
    isTSX: true,
    allExtensions: true,
  },
]);
babelConfig.plugins.push([babelPluginImport, { libraryName: 'antd', libraryDirectory: 'es' }]);

const config = {
  entry: {
    index: path.resolve(__dirname, '../src/index.js'),
  },
  stats: { children: false },
  output: {
    path: path.join(__dirname, '../dist'),
    filename: '[name].[chunkHash].js',
    chunkFilename: '[name].[chunkHash].js',
    publicPath: publicPath,
  },
  externals: {
    '@antv/data-set': 'DataSet',
  },
  module: {
    rules: [
      {
        test: /\.(css|less)$/,
        use: [
          devMode ? 'style-loader' : 
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              modules: {
                localIdentName: '[local]_[hash:base64:6]',
              },
            },
          },
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                javascriptEnabled: true,
                modifyVars: theme,
              }   
            },
          },
        ],
        exclude: [path.resolve(__dirname, '..', 'node_modules')],
      },
      {
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: babelConfig,
        },
      },
      {
        test: /\.ejs$/,
        exclude: /node_modules/,
        loader: 'ejs-loader',
        options: {
          variable: 'data',
          interpolate : '\\{\\{(.+?)\\}\\}',
          evaluate : '\\[\\[(.+?)\\]\\]'
        }
      },
      {
        test: /\.(png|jpg|svg|gif|ico)$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
        },
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'file-loader',
      },
      {
        test: /\.(css|less)/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                javascriptEnabled: true,
                modifyVars: theme,
              }  
            },
          },
        ],
        include: [path.resolve(__dirname, '..', 'node_modules')],
      },
      {
        test: /\.(ts|js)x?$/,
        include: [
          path.resolve(__dirname, '..', 'node_modules/@antv/layout'),
          path.resolve(__dirname, '..', 'node_modules/fast-deep-equal'),
          path.resolve(__dirname, '..', 'node_modules/ml-matrix'),
          path.resolve(__dirname, '..', 'node_modules/@dnd-kit/utilities'),
          path.resolve(__dirname, '..', 'node_modules/@dnd-kit/core'),
        ],
        use: {
          loader: 'babel-loader',
          options: babelConfig,
        },
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    alias: {
      src: path.resolve(__dirname, '../src'),
      node_modules: path.resolve(__dirname, '../node_modules'),
      assets: path.resolve(__dirname, '../src/assets'),
      common: path.resolve(__dirname, '../src/common'),
      components: path.resolve(__dirname, '../src/components'),
      utils: path.resolve(__dirname, '../src/utils'),
      models: path.resolve(__dirname, '../src/models'),
      routes: path.resolve(__dirname, '../src/routes'),
      layouts: path.resolve(__dirname, '../src/layouts'),
      services: path.resolve(__dirname, '../src/services'),
    },
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: '!!ejs-loader!./src/index.ejs',
    }),
    new MiniCssExtractPlugin({
      ignoreOrder: true,
      filename: '[name].css',
      chunkFilename: '[name].[chunkhash].css',
    }),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
    new webpack.DefinePlugin({
      'process.env.BACKENDENV': JSON.stringify(backEndEnv),
      STATIC_URL: JSON.stringify(`https://${urlEnv}static.ymm56.com`),
    }),
  ],
};

module.exports = config;
